const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderSn:'',
    data: {},
    points: [],
    taskId: '',
    backHome: 10,
    detailData: {},
    goodTypeData: [],
    isShowRecord: false,
    showType: '',
    isShowOpacity: false,
    isShowModal: false,
    clickWhich: 0,
    screenHeight: 0,
    navigationSetting: {
      title: '订单详情',
      navigationBarTextStyle: "white",
      height: app.globalData.navBarHeight,
      paddingTop: app.globalData.statusBarHeight,
      backgroundColor: '#c8102e',
      size: 'default',
      back: 'true',
      expireTime: '',
      status: ''
    },
    processData: []
  },
  fly() {

  },
  showRecord() {
    this.setData({
      isShowRecord: true
    })
  },
  onUnload: function () {
    wx.switchTab({
      url: '/pages/tasks/tasks',
    })
  },


  closeProcess() {
    console.log(3232)
    this.setData({
      showType: 'progress'
    })
    wx.setPageStyle({
      style: {
        overflow: ''
      }
    })
    this.setData({
      isShowOpacity: false
    })
    this.setData({
      isShowModal: false
    })
  },
  countdown() {
    console.log('dsdsd')
    let time = this.data.time
    time = time - 1000
    this.setData({
      time
    })
    let minutes = parseInt((time % 3600000) / 60000);
    let seconds = parseInt((time % 60000) / 1000);
    if (minutes < 0 || seconds < 0) {
      this.setData({
        'detailData.orderStatus': 7
      })
      app.task.expireOrder({
        id: this.data.taskId
      }).then(res => {

      })
    } else {
      minutes < 10 ? minutes = '0' + minutes : '';
      seconds < 10 ? seconds = '0' + seconds : '';

      this.setData({
        timeTxt: minutes + ':' + seconds
      })
      if (time > 1) {
        setTimeout(this.countdown, 1000);
      }
    }


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setInterval = setInterval(() => {
      this.setData({ refresh: Date.now() }) // 触发视图更新
    }, 1000)
    console.log(options)

    let orderSn = options.orderno
    this.setData({
      orderSn
    })
    this.initData(orderSn)

  },
  reflesh(){
    this.initData(this.data.orderSn)
    wx.showToast({
      title: '已刷新',
      icon: 'success'
    })
  },
  initData(orderSn){
    let _this = this
    console.log(323)
    app.order.getOrderDetail({
      orderSn: orderSn
    }).then(res => {
      if (res.code === 200) {
        _this.setData({ data: res.data })
        _this.setData({ 'navigationSetting.expireTime': res.data.expireTime })
        _this.setData({ 'navigationSetting.status': res.data.status })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  viewProgress() {
    this.setData({
      showType: 'progress'
    })
    wx.setPageStyle({
      style: {
        overflow: 'hidden'
      }
    })
    this.setData({
      isShowOpacity: true
    })
    this.setData({
      isShowModal: true
    })
    let _this = this
    app.task.getTaskStatusLog({
      taskId: this.data.taskId
    }).then((res) => {
      if (res.code === 200) {
        _this.setData({
          processData: res.data
        })
      }
    })
  },
  updateGoods() {

    this.setData({
      clickWhich: 1,
      showType: 'goods',
      isShowOpacity: true,
      isShowModal: true
    })

    wx.setPageStyle({
      style: {
        overflow: 'hidden'
      }
    })



  },
  copyTaskNum(e) {
    wx.setClipboardData({
      data: e.target.dataset.word,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            console.log(res.data)
          }
        })
      },
    })
  },
  confirmSubmit(e) {
    let _this = this
    let formData = {}
    formData.taskId = this.data.taskId
    formData.goodsName = e.detail.name
    formData.goodsTypeId = e.detail.goodsTypeId
    formData.goodsTypeName = e.detail.goodsTypeName
    formData.recordArr = e.detail.recordArr
    app.task.updateGoods(formData).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: '修改成功',
        })
        _this.setData({
          isShowOpacity: false,
          isShowModal: false,
          'detailData.goodsTypeResponse.goodsTypeName': formData.goodsTypeName,
          'detailData.goodsTypeResponse.name': formData.goodsName,
          'detailData.goodsTypeResponse.goodsTypeId': formData.goodsTypeId,
          clickWhich: 0
        })
        wx.setPageStyle({
          style: {
            overflow: ''
          }
        })



      } else if (res.code === 40000) {
        if (res.data === 'NOT_WAIT_CONFIRM') {
          wx.showToast({
            title: '仅当状态为待确认才能修改',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '修改失败',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '修改失败',
          icon: 'none'
        })
      }
    })
  },
  confirmIsShowModal(e) {
    this.setData({
      isShowModal: false,
      isShowOpacity: false,
      clickWhich: 0
    })
    wx.setPageStyle({
      style: {
        overflow: ''
      }
    })
  },

  cancelOrder() {
    console.log(21221)
    let _this = this
    wx.showModal({
      title: '确认取消？', //提示的标题
      content: '', //提示的内容
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击了确定')
          app.task.cancelOrder({
            id: _this.data.data.id
          }).then(res => {
            if (res.code === 200) {

              wx.showToast({
                title: '取消成功',
              })
              _this.initData(_this.data.orderSn)
              // _this.setData({
              //   'data.status': 6,
              //   'data.updatedAt':res.data
              // })
            } else if (res.code === 40000) {
              if (res.data === 'NOT_WAIT_CONFIRM') {
                wx.showToast({
                  title: '仅当状态为待确认才能取消',
                  icon: 'none'
                })
              } else {
                wx.showToast({
                  title: '取消失败',
                  icon: 'none'
                })
              }
            } else {
              wx.showToast({
                title: '取消失败',
                icon: 'none'
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击了取消')
        }
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  handlePay: function (e) {
    const id = e.currentTarget.dataset.id;
    let params = {}
    params.orderId = this.data.data.id
    let _this = this
    app.task.getPaySettings(params).then(res => {
      if (res.code == 200) {
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: 'MD5',
          paySign: res.data.paySign,
          success(res) {
            // _this.setData({
            //   'data.status': 0
            // })
            wx.showToast({
              title: '支付成功',
            })
            _this.initData(_this.data.orderSn)
          },
          fail(err) {
          }
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
